
import aiomysql
from fastapi import FastAPI
from loguru import logger

from app.core.config import HOST, PORT, USERNAME, DATABASE, DATABASE_URL

from app.db.database import (
    db, reset_db_state, get_db
)

from app.db import schemas

async def connect_to_db(app: FastAPI) -> None:
    logger.info("Connecting to {0}", repr(DATABASE_URL))
    logger.info("HOST: {0}", repr(HOST))
    logger.info("USER: {0}", repr(USERNAME))
    logger.info("DATABASE: {0}", repr(DATABASE))
    db.connect()
    # 创建数据库表
    schemas.create_tables(db)

    logger.info("Connection established")


async def close_db_connection(app: FastAPI) -> None:
    logger.info("Closing connection to database")
    db.close()
    logger.info("Connection closed")
